 var appPath = getAppPath();

$(function(){
	// 批次号
	$("#queryYhLot").combobox({
		url:appPath+'/itbCouponStats/getLotNoForCombo.action?blkFlg=1',
		valueField:'id',
		textField:'name',
		onSelect:function(selRecdM){
			
		}
	});
	// 持有方(检索用)
	$("#queryYhOrg").combobox({
		url:appPath+'/itbCouponStats/getOrgForCombo.action?blkFlg=1',
		valueField:'org_id',
		textField:'name',
		onSelect:function(selRecd){
			$("#queryYhOwner").combobox("clear");
			$("#queryYhOwner").combobox("loadData",{});
			
			var orgId=selRecd.org_id;
			if(Number(orgId||'0')>0) {
				$("#queryYhOwner").combobox({
					url:appPath+'/itbCouponStats/getOrgUserForCombo.action?blkFlg=1&orgId='+orgId,
					valueField:'id',
					textField:'name',
					onSelect:function(selRecdU){
						// TODO
					}
				});
			}
		}
	});
	initJspGrid();
	initUserCntGrid();
	initOrderCntGrid();
	initPayCntGrid();
});
//导出
function exportCoupon(){
		var strDate=$.DateUtil.format(new Date(),'yyyyMMddhhmmss');
		var fNm = "ITB_COUPON_STATS_"+strDate+".csv";
		var aLink = $("#downA");//将地址复制给下载方
		aLink.html(fNm);
		aLink.attr("href", appPath+"/itbCouponStats/downCouponInfo.action");

		$("#downBtn").show();
		$('#downDlg').window({
			title:'导出优惠券信息',
			iconCls:'icon-edit',
			modal: true,
			shadow: true,
			collapsible:false,
			minimizable:false,
			maximizable:false
		});
		
		$('#downDlg').window('move',{top:20,left:20});
		$('#downDlg').window('open');

}
function cancelDown(){
	$('#downDlg').window('close');
}
// 样版列表初始化
function initJspGrid(){
	$('#couponGrid').datagrid({
	    url:appPath + "/itbCouponStats/getCouponStatsInfoPg.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'org_name',title:'持有组织',width:60,align:'left',sortable:false},
	        {field:'nickname',title:'持有人',width:60,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		return rowData.owner+': '+value;
	        	}
	        },
	        {field:'lot_no',title:'批次号',width:60,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		if($("#queryYhLot").combobox('getValue')>0
	        			||!$("#queryCouponName").val()==""||
	        			 $("#queryYhOrg").combobox('getValue')>0
	        			 ||$("#queryYhOwner").combobox('getValue')>0) {
	        			return value;
	        		}
	        			return 'N/A'
	        	}
	        },
	        {field:'couponCnt',title:'持有券数',width:60,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		return showNum(value, 'green');
	        	}
	        },
	        {field:'convert_coupon',title:'兑换券数',width:60,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		return showNum(value, 'blue');
	        	}
	        },
	        {field:'usercnt',title:'兑换用户数',width:60,align:'left',sortable:false,
	  	   	  formatter:function(value, rowData, rowIndex){
	  	   	
	        	 return "<a href='javascript:void(0)' onClick='editUsercnt("+rowData.org_id+","+rowData.owner+")'>"+value+"</a>";	
	  	   	  }
	        },
	        {field:'ordercnt',title:'下单数',width:60,align:'left',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){ 	  
		  	   		  return "<a href='javascript:void(0)' onClick='editOrdercnt("+rowData.org_id+","+rowData.owner+")'>"+value+"</a>";	
			  	  }
	        },
	        {field:'goodscnt',title:'支付订单数',width:60,align:'left',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){ 	  
		  	   		  return "<a href='javascript:void(0)' onClick='editPaycntInfo("+rowData.org_id+","+rowData.owner+")'>"+value+"</a>";	
			  	  }
	        },
	        {field:'summoney',title:'支付订单总金额',width:60,align:'left',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		if(Number(rowData.goodscnt||'0')>0) {
	        			return Number(value||'0')/100 + ' 元';
	        		}
	        		return 'N/A';
	        	}
	        },
	        {field:'desc',title:'备注',width:60,align:'left',sortable:false,
	         	formatter:function(value, rowData, rowIndex){
	        		if(!$("#queryCouponName").val()==""
	        		|| $("#queryYhLot").combobox('getValue')>0
	        		|| $("#queryYhOwner").combobox('getValue')>0
	        		|| $("#queryYhOrg").combobox('getValue')>0) {
	        			return rowData.desc;
	        		}
	        		return 'N/A'
	        	}
	        }
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[
			{
				text:'全部导出',
				iconCls:'icon-add',
				handler:function(){
					exportCoupon();

				}
			}
		]
	});
}

		//初始加載用戶統計列表
function initUserCntGrid(){
	$('#UserCntGrid').datagrid({
	    url:appPath + "/itbCouponStats/getUserCntInfoLst.action",
	    height:'360',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam2(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'user_id',title:'用户ID',width:60,align:'left',sortable:false},
	        {field:'user_name',title:'用户名',width:60,align:'left',sortable:false,
		      	  formatter:function(value, rowData, rowIndex){
		    		  if(value&&value!==''&&value!==undefined){
		    			   return rowData.name
		    		  }
		    		  return 'N/A'
		    	  }
		      },
	        {field:'nickname',title:'昵称',width:60,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(value){
		    			   return rowData.nickname;
		    		  }
		    		  return 'N/A'
		    	  }
		      },
	        {field:'mobile',title:'手机号',width:80,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(value){
		    			  return rowData.mobile
		    		  }
		    		  return 'N/A'
		    	  }
	        }
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[]
	    
	});
}
function initOrderCntGrid(){
	$('#OrderCntGrid').datagrid({
	    url:appPath + "/itbCouponStats/getOrderCntInfoLst.action",
	    height:'360',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam2(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'trade_obj_id',title:'订单ID',width:60,align:'left',sortable:false},
	        {field:'user_name',title:'用户名',width:60,align:'left',sortable:false,
		      	  formatter:function(value, rowData, rowIndex){
		    		  if(value){
		    			   return rowData.name
		    		  }
		    		  return 'N/A'
		    	  }
		      },
	        {field:'nickname',title:'昵称',width:60,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(value){
		    			   return rowData.nickname;
		    		  }
		    		  return 'N/A'
		    	  }
		      },
	        {field:'mobile',title:'手机号',width:80,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(value){
		    			  return rowData.mobile
		    		  }
		    		  return 'N/A'
		    	  }
	        },
	       {field:'c_id',title:'优惠券ID',width:60,align:'left',sortable:false},

	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[]
	    
	});

}
function initPayCntGrid(){
	$('#PayCntGrid').datagrid({
	    url:appPath + "/itbCouponStats/getPayCntInfoLst.action",
	    height:'360',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam2(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	  	        {field:'trade_obj_id',title:'订单ID',width:60,align:'left',sortable:false},
	  	        {field:'user_id',title:'用户ID',width:60,align:'left',sortable:false,
	  		      	  formatter:function(value, rowData, rowIndex){
	  		    		  if(value){
	  		    			   return rowData.user_id
	  		    		  }
	  		    		  return 'N/A'
	  		    	  }
	  		      },
	  	        {field:'mobile',title:'手机号',width:80,align:'left',sortable:false,
	  		    	  formatter:function(value, rowData, rowIndex){
	  		    		  if(value){
	  		    			  return rowData.mobile
	  		    		  }
	  		    		  return 'N/A'
	  		    	  }
	  	        },
	  	        {field:'total_fee',title:'总额',width:60,align:'left',sortable:false,
	  	        	formatter:function(value, rowData, rowIndex){
		        		if(Number(rowData.total_fee||'0')>0) {
		        			return Number(value||'0')/100 + ' 元';
		        		}
		        		return 'N/A';
		        	}
	  		      },
	  		    {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false,
	  		    	formatter:function(value, rowData, rowIndex){
	  		    		if(value){
	  		    			return value.substr(0,10);
	  		    		}
	  		    		return 'N/A'
	  		        }
	  		    },		    
		  	{field:'c_id',title:'优惠券ID',width:60,align:'left',sortable:false},

	  	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[]
	});

}
function showNum(value, color) {
	var show = Number(value||'0') || 0;
	if(show > 0) {
		return '<span style="color:'+(color||'black')+'">'+Number(value||'0')+'</span>';
	}
	return show;
}
function getState4Show(value){
	var state = '<span style="color:red">未知</span>';
	switch(Number(value||'0')){
		case 10: state='<span style="color:red">直接抵扣</span>'; break;
		case 20: state='<span style="color:green">满减券</span>'; break;
		case 30: state='<span style="color:brown">折扣券</span>'; break;
	}
	return state;
}
//編輯展示用戶
function editUsercnt(orgId,owner){
	//alert(orgId+" "+owner);
	$("#orgId").val(orgId);
	$("#owner").val(owner);
	
	$("#UserCntGrid").datagrid('unselectAll');
	$("#UserCntGrid").datagrid('load', getQueryParam2());
	$("#tmplBtn").show();
	$('#editUserCnt').window({
		title:'用户列表',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editUserCnt').window('move',{top:20,left:20});
	$('#editUserCnt').window('open');
}
function cancelUpdTmpl(){
	$('#editUserCnt').window('close');
}
	
function editOrdercnt(orgId,owner){
	$("#orgId").val(orgId);
	$("#owner").val(owner);
	$("#OrderCntGrid").datagrid('unselectAll');
	$("#OrderCntGrid").datagrid('load', getQueryParam2());
	$("#tmplBtn").show();
	$('#editOrderCnt').window({
		title:'用户列表',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editOrderCnt').window('move',{top:20,left:20});
	$('#editOrderCnt').window('open');
}

function cancelUpdTmpl2(){
	$('#editOrderCnt').window('close');
}

function editPaycntInfo(orgId,owner){
	$("#orgId").val(orgId);
	$("#owner").val(owner);
	
	$("#PayCntGrid").datagrid('unselectAll');
	$("#PayCntGrid").datagrid('load', getQueryParam2());
	
	$("#tmplBtn").show();
	$('#editPayCnt').window({
		title:'用户列表',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editPayCnt').window('move',{top:20,left:20});
	$('#editPayCnt').window('open');
	
}

function cancelUpdTmpl3(){
	$('#editPayCnt').window('close');
}

function getQueryParam(){
	var queryObject = {};
	var findOrg = $("#queryYhOrg").combobox('getValue');
	var findOwner = $("#queryYhOwner").combobox('getValue');
	var findLot= $("#queryYhLot").combobox('getValue');
	var findCouponName=$("#queryCouponName").val().trim();//文档名字
	
	if(findOrg && Number(findOrg||'0')>0) queryObject.findYhOrg = findOrg;// 所有组织
	if(findOwner && Number(findOwner||'0')>0) queryObject.findYhOwner = findOwner;// 所有者
	if(findLot && Number(findLot||'0')>0) queryObject.findYhLot = findLot;// 批次
	if(findCouponName&&findCouponName.length>0) queryObject.findCouponName = findCouponName;// 描述
	
	return queryObject;
}
/**
 * 查询数据
 */
function doQuery(){
	$("#couponGrid").datagrid('unselectAll');
	$("#couponGrid").datagrid('load', getQueryParam());
}

function getQueryParam2(){
	var queryObject = {
			orgId: Number($("#orgId").val()||'0'),
			owner: Number($("#owner").val()||'0'),
			findYhLot: Number($("#queryYhLot").combobox('getValue')||'0'),
			findCouponName:$("#queryCouponName").val().trim()
	};
	
	return queryObject;
}




